package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "套餐管理")
@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("分页查询{}",setmealPageQueryDTO);
    PageResult pageResult= setmealService.page(setmealPageQueryDTO);
    return Result.success(pageResult);

    }


    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @CacheEvict(cacheNames = "setmeal",key = "setmealDTO.categoryId")//清理指定条件下的缓存数据
    @ApiOperation("新增套餐")
    @PostMapping
    public Result add(@RequestBody SetmealDTO setmealDTO){
        log.info("新增菜品管理{}",setmealDTO);
        setmealService.insert(setmealDTO);
        return Result.success();
    }

    /**
     * 根据ID查询套餐
     * @param id
     * @return
     */
    @ApiOperation("根据ID查询套餐")
    @GetMapping("/{id}")
    public Result select(@PathVariable Long id){
        log.info("根据id查询套餐");
    SetmealVO setmealVO = setmealService.selectById(id);
    return Result.success(setmealVO);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @CacheEvict(cacheNames = "setmeal",allEntries = true)//清理指定目录下的全部数据
    @ApiOperation("修改套餐")
    @PutMapping
    public  Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐",setmealDTO);
        setmealService.updateSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐的起售和停售
     *
     * @param status
     * @param id
     * @return
     */
    @CacheEvict(cacheNames = "setmeal",allEntries = true)//清理指定目录下的全部数据
    @ApiOperation("套餐起售、停售")
    @PostMapping("/status/{status}")
    public Result stop(@PathVariable Integer status,Long id){
        log.info("修改套餐",status,id);
        setmealService.enDisable(status,id);
        return Result.success();
    }

    /**
     * 删除套餐
     *
     * @param ids
     * @return
     */
    @CacheEvict(cacheNames = "setmeal",allEntries = true)//清理指定目录下的全部数据
    @ApiOperation("删除套餐")
    @DeleteMapping()
    public  Result delete(@RequestParam  List<Long> ids){
        log.info("删除套餐",ids);
        setmealService.delete(ids);
    return Result.success();
    }
}
